import Vue from 'vue'
import Vuex from 'vuex'
import request from '@/common/request'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		// 存储大量 list 数据的容器
		largeList: null,
		playIndex: 0,
		sortParams: {},
		filterParams: {},
		groupFilterParams: {},
		httpParams: {},
		token: '',
		delVideoId: '',
		user: {},
		favoriteItem: {},
		filterFormCache: {}, // 用于缓存filterForm的对象
		theme: 'dark' // light/dark
	},
	mutations: {
		setTheme(state, theme) {
		  state.theme = theme
		 //  console.log(uni.getSystemInfoSync().platform)
		 //  // 切换根元素类名（H5 端操作 html/body，小程序/App 操作 page）
		 //  const rootEl = uni.getSystemInfoSync().platform === 'h5' 
			// ? document.documentElement
			// : getCurrentPages()[getCurrentPages().length - 1].$el;
		  
		 //  rootEl.classList.remove('light-theme', 'dark-theme');
		 //  rootEl.classList.add(`${theme}-theme`);
		 //  console.log("当前主题是",theme)
		  // 持久化存储
		  uni.setStorageSync('theme', theme);
		},
		// 保存filterForm到缓存
		saveFilterFormCache(state, { key, form }) {
		  state.filterFormCache[key] = { ...form };
		},
		// 清除指定key的缓存
		clearFilterFormCache(state, key) {
		  if (key) {
			delete state.filterFormCache[key];
		  } else {
			state.filterFormCache = {};
		  }
		},
		token(state, token) {
			state.token = token
		},
		user(state, user) {
			state.user = user
		},
		sortParams(state, sortParams) {
			state.sortParams = sortParams
		},
		filterParams(state, filterParams) {
			state.filterParams = filterParams
		},
		groupFilterParams(state, groupFilterParams) {
			state.groupFilterParams = groupFilterParams
		},
		filterFormCache(state, filterFormCache) {
			state.filterFormCache = filterFormCache
		},
		httpParams(state, httpParams) {
			state.httpParams = httpParams
		},
		logout(state) {
			state.token = ''
			state.user = {}
		},
		// 保存数据到 state
		SET_LARGE_LIST(state, {data, index, httpParams}) {
			state.largeList = data
			state.playIndex = index || 0
			state.httpParams = httpParams
		},
		// 清空数据（释放内存）
		CLEAR_LARGE_LIST(state) {
			state.largeList = null
			state.playIndex = 0
			state.httpParams = {}
		},
		favoriteItem(state, favoriteItem) {
		  state.favoriteItem = favoriteItem;
		},
		delVideoId(state, delVideoId) {
		  state.delVideoId = delVideoId;
		},
		clearAllData(state) {
		  state.filterFormCache = {};
		  uni.setStorageSync('filterFormCache',"{}")
		  state.sortParams = {};
		  uni.setStorageSync('sortParams',"{}")
		  state.filterParams = {};
		  uni.setStorageSync('filterParams',"{}")
		  state.groupFilterParams = {};
		  uni.setStorageSync('groupFilterParams',"{}")
		}
	},
	actions: {
		async autoLogin(context) {
			if (!context.state.token) {
				const token = uni.getStorageSync('token')
				const user = uni.getStorageSync('user')
				if (user) {
					context.commit('user', JSON.parse(user))
				}
				if (token) {
					context.commit('token', token)
				} else {
					uni.reLaunch({
						url: '/pages/profile/login'
					})
				}
				let sortParams = uni.getStorageSync('sortParams') || "{}"
				context.commit('sortParams', JSON.parse(sortParams))
				let filterParams = uni.getStorageSync('filterParams') || "{}"
				context.commit('filterParams', JSON.parse(filterParams))
				let groupFilterParams = uni.getStorageSync('groupFilterParams') || "{}"
				context.commit('groupFilterParams', JSON.parse(groupFilterParams))
				let filterFormCache = uni.getStorageSync('filterFormCache') || "{}"
				context.commit('filterFormCache', JSON.parse(filterFormCache))
			}
		},
		// 异步保存数据（适合从接口获取数据后调用）
		saveLargeList({
			commit
		}, {data,index,httpParams}) {
			commit('SET_LARGE_LIST', {data, index, httpParams})
		},
		// 异步清空数据
		clearLargeList({
			commit
		}) {
			commit('CLEAR_LARGE_LIST')
		}
	},
	getters: {
		// 获取数据的快捷方式
		getLargeList: state => state.largeList,
		getPlayIndex: state => state.playIndex,
		getHttpParams: state => state.httpParams
	}
})
export default store